package com.amar.dtadmin.apidoc.action;

import cn.hutool.core.util.StrUtil;
import com.amar.dtadmin.apidoc.fegin.DtAdminClient;
import com.amar.dtadmin.apidoc.util.PsiAnnotationUtil;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

/**
 * @author xiaoshiqiang
 * @since 2025/2/5
 */
@Slf4j
public class ApiDocUploadAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent event) {
        DtAdminApiDocActionGroup.SelectedPsiData selectedPisData = DtAdminApiDocActionGroup.getSelectedPisData(event);
        PsiAnnotation apiAnnotation = selectedPisData.getApiAnnotation();
        String group = PsiAnnotationUtil.getAnnotationValue(apiAnnotation, "group");
        String api = PsiAnnotationUtil.getAnnotationValue(apiAnnotation, "name");
        String name = PsiAnnotationUtil.getAnnotationValue(apiAnnotation, "desc");
        if (StrUtil.isEmpty(group)) {
            Messages.showWarningDialog("API分组为空，请检查@ApiService注解的group属性", "注解属性缺失");
            return;
        }
        if (StrUtil.isEmpty(api)) {
            Messages.showWarningDialog("API编号为空，请检查@ApiService注解的name属性", "注解属性缺失");
            return;
        }
        if (StrUtil.isEmpty(name)) {
            Messages.showWarningDialog("API名称为空，请检查@ApiService注解的desc属性", "注解属性缺失");
            return;
        }
        // 上传API文档
        new DtAdminClient(event.getProject()).uploadApiDoc(DtAdminApiDocActionGroup.getSelectedPisData(event).getApiInfo());
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        super.update(e);
        e.getPresentation().setEnabledAndVisible(DtAdminApiDocActionGroup.getSelectedPisData(e).isValid());
    }

    @Override
    public @NotNull ActionUpdateThread getActionUpdateThread() {
        return ActionUpdateThread.BGT;
    }
}
